package com.tangk.tool.author.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.dom4j.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.DocumentHandler;
import org.xml.sax.SAXException;

import com.tangk.proxy.DefaultProxHandler;
import com.tangk.proxy.ProxyMeta;
import com.tangk.tool.author.AuthorTools;
import com.tangk.tool.util.ReflectUtils;

public class Config {
	static Properties p = new Properties();
	static {
		p.put("authTools", AuthorTools.class.getName());
	}
	static org.w3c.dom.Document document = null;
	static Map<String, List<ProxyMeta>> filterMap;
	static{
		try {
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		      factory.setIgnoringElementContentWhitespace(true);
		       //通过工厂获取DocumentBuilder实例
		       DocumentBuilder dbuilder=factory.newDocumentBuilder();
			document = dbuilder.parse("defaultConfigPath.xml");
			System.out.println(document.getBaseURI());
		} catch (SAXException | IOException | ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*	Element root = document.createElement("root");
			document.appendChild(root);
			Element filters = document.createElement("filters");
			root.appendChild(filters);
			Element filter = document.createElement("filter");
			filter.setAttribute("name", "DefaultProxHandler");
			filter.setAttribute("ref", DefaultProxHandler.class.getName());
			filter.setAttribute("beforeMehod", "beforeMehod");
			filter.setAttribute("endMehod", "endMehod");
			filters.appendChild(filter);*/
		
		
		filterMap = new HashMap<>();
		ArrayList<ProxyMeta> before = new ArrayList<ProxyMeta>();
		ArrayList<ProxyMeta> end = new ArrayList<ProxyMeta>();
		filterMap.put("before", before);
		filterMap.put("end", end);
		Element rootElement = (Element) document.getElementsByTagName("root").item(0);
		Element filterElement = (Element) rootElement.getChildNodes().item(0);
		NodeList filterList = filterElement.getChildNodes();
		ProxyMeta proxyMeta ;
		for(int i = 0;i < filterList.getLength();i++){
			Element filter = (Element) filterList.item(i);
			
			String beforeMethod = filter.getAttribute("beforMethod");
			String endMethod = filter.getAttribute("endMehod");
			String objClassName = filter.getAttribute("ref");
			Object obj = ReflectUtils.newInstance(objClassName);
			if(beforeMethod != null && beforeMethod!=""){
				proxyMeta = new ProxyMeta();
				proxyMeta.setMethodName(beforeMethod);
				proxyMeta.setTartget(obj);
				before.add(proxyMeta);
			}
			if(endMethod != null && endMethod!=""){
				proxyMeta = new ProxyMeta();
				proxyMeta.setMethodName(endMethod);
				proxyMeta.setTartget(obj);
				end.add(proxyMeta);
			}
			
			//filterMap.put(key, value)
		}
	}
	public static String get(String configName,String defaultValue){
		String rst = p.getProperty(configName);
		return Utils.returnResult(rst,defaultValue);
	}
	
	public static List<ProxyMeta> getBeforFilter() {
		
		return filterMap.get("before");
	}

	public static List<ProxyMeta> getEndFilter() {
		// TODO Auto-generated method stub
		return filterMap.get("end");
	}
}
